package service

import (
	"fmt"
	"time"
	"usplatformserver/internal/model"

	"github.com/tealeg/xlsx"
)

type ProductUsageRateRequest struct {
	BeginTime string `form:"beginTime"`
	EndTime   string `form:"endTime"`
}

var productUsageSheetHeaders = []string{
	"序号", "检查时间", "患者登记量(人)", "平均检查时间(s)", "使用率",
}

func (svc *Service) GetUsageData(patientIds, startTime, endTime string, count int) ([]model.ProductUsageRateInfo, error) {

	fmt.Println("count: ", count)
	//获取使用率数据
	usageData, err := svc.dao.GetUsageExportData(patientIds)
	fmt.Println("usageData size: ", len(usageData))

	if nil != err {
		return nil, err
	}

	var usageRates []model.ProductUsageRateInfo
	//拼接使用数据
	var usage model.ProductUsageRateInfo

	var checkStartDateTime string = ""
	var checkEndDateTime string = ""
	var checkCount int = 0
	var checkSpendTime int = 0
	var isUsed bool = false
	var usedCount int = 0
	for i, v := range usageData {
		//fmt.Println("patientId: ", v.PatientId, " v.CheckDateTime: ", v.CheckDateTime)
		if v.EventId == 3 {
			isUsed = true
		}
		if v.EventId == 6 {
			isUsed = true
		}
		if v.EventId == 22 {
			isUsed = true
		}
		if v.EventId == 23 {
			isUsed = true
		}
		if v.EventId == 2 {
			if checkStartDateTime != "" && v.CheckDateTime != checkStartDateTime {
				checkEndDateTime = FormatDatetime(v.CheckDateTime)
				checkSpendTime += int(GetCheckCostTimeByLayout(checkStartDateTime, checkEndDateTime, "2006-01-02 15:04:05"))
				fmt.Println("startDateTime: ", checkStartDateTime, " endDateTime: ", checkEndDateTime)
				fmt.Println("usage.CheckSpendTime: ", checkSpendTime)
				//usage.Model = &model.Model{ID: uint32(v.PatientId)}
				if isUsed {
					usedCount++
				}
				checkCount++
				isUsed = false
			}
			checkStartDateTime = FormatDatetime(v.CheckDateTime)
		}
		if i == (len(usageData) - 1) {
			//最后一条数据无法统计结束时间，默认为空
			checkCount++
			if isUsed {
				usedCount++
			}
		}
	}
	usage.PatientCount = checkCount
	if checkCount > 0 {
		usage.CheckSpendTime = checkSpendTime / checkCount
	}
	usage.Usage = 0
	if checkCount > 0 {
		usage.Usage = float32(usedCount) / float32(checkCount)
	}
	usage.CheckStartDateTime = startTime
	usage.CheckEndDateTime = endTime
	usageRates = append(usageRates, usage)

	fmt.Println("product usage len: ", len(usageRates))

	return usageRates, nil
}

func (svc *Service) ExportUsageData(data []model.ProductUsageRateInfo) (string, error) {
	//创建excel文件
	file := xlsx.NewFile()
	//添加表
	sheet, err := file.AddSheet("产品使用率")
	if nil != err {
		return "", err
	}
	//添加标题行
	row := sheet.AddRow()
	//添加列标
	for _, v := range productUsageSheetHeaders {
		cell := row.AddCell()
		cell.Value = v
	}

	//添加数据
	for i, v := range data {
		row := sheet.AddRow()
		//序号
		appendCell(row, fmt.Sprintf("%v", i+1))
		//检查时间
		appendCell(row, fmt.Sprintf("%v - %v", v.CheckStartDateTime, v.CheckEndDateTime))
		//患者登记量
		appendCell(row, fmt.Sprintf("%v", v.PatientCount))
		//平均检查时间
		appendCell(row, fmt.Sprintf("%v", v.CheckSpendTime))
		//使用率
		appendCell(row, fmt.Sprintf("%v", v.Usage*100)+"%")
	}
	fileName := fmt.Sprintf("usage_%d.xlsx", time.Now().UnixMilli())
	//保存文件
	file.Save(fileName)

	return fileName, nil
}

// func getUsageString(isUsed int) string {
// 	if isUsed == 1 {
// 		return "已使用"
// 	} else {
// 		return "未使用"
// 	}
// }
